<?php

/*
 * Usuarios
 * Listado de Usuarios
 * ABM de usuarios
*/

class Usuarios extends Controller {

    function Cursos() {
        parent::Controller();
        $this->load->library('session');
        if(!$this->session->userdata('logged_in'))
            redirect('/home/');
        $this->load->language('general',$this->session->userdata('lenguaje'));
    }

    function index() {
        $datos['titulo_pagina'] = 'Usuarios';
        $this->load->view('encabezado_admin.php', $datos);
        $this->load->view('admin/menu_admin.php');
        $this->load->view('admin/usuarios.php');
        $this->load->view('pie_admin.php');
    }

    function generar_tarjeta() {
        $id = $this->uri->segment(4);
        $datos['id'] = $id;
        $datos['titulo_pagina'] = 'Usuarios';
        $this->load->view('encabezado_admin.php', $datos);
        $this->load->view('admin/menu_admin.php');
        $this->load->view('admin/am_tarjetas.php',$datos);
        $this->load->view('pie_admin.php');
    }

    function guardar_tarjeta() {
        $id = $this->input->post('id');
        $datos1 = $this->input->post('datos1');
        $query = $this->db->query("UPDATE users set params='".$datos1."' WHERE id=".$id);
        redirect('/admin/admin');
    }

    function post() {
        $tabla       = "users";
        $page        = $this->input->post('page');
        $limit       = $this->input->post('rows');
        $sidx        = $this->input->post('sidx');
        $sord        = $this->input->post('sord');
        $_search     = $this->input->post('_search');
        $searchField = $this->input->post('searchField');
        $searchString= $this->input->post('searchString');

        $where = "";
        if ($_search=='true')
            $where = " WHERE $searchField LIKE '%$searchString%' ";


        if(!$sidx) $sidx =1;

        $sql    = "SELECT * FROM $tabla $where";
        //echo $sql;
        $query  = $this->db->query($sql);
        $result = $query->result();

        $count  = count($result);

        if( $count > 0 && $limit > 0) {
            $total_pages = ceil($count/$limit);
        } else {
            $total_pages = 0;
        }

        if ($page > $total_pages) $page=$total_pages;
        $start = $limit*$page - $limit;
        if($start <0) $start = 0;

        $sql    = "SELECT * FROM $tabla $where ORDER BY $sidx $sord LIMIT $start , $limit";
        $query  = $this->db->query($sql);
        $result = $query->result();


        header("Expires: Mon, 26 Jul 1997 05:00:00 GMT" );
        header("Last-Modified: " . gmdate( "D, d M Y H:i:s" ) . "GMT" );
        header("Cache-Control: no-cache, must-revalidate" );
        header("Pragma: no-cache" );
        header("Content-type: text/x-json");
        $i=0;
        $data['page']= $page;
        $data['total']= $total_pages;

        foreach ($query->result() as $row) {
            $data['rows'][$i]['id']= $row->id;
            $data['rows'][$i]['cell'][]= $row->id;
            $data['rows'][$i]['cell'][]= utf8_encode($row->name);
            $data['rows'][$i]['cell'][]= utf8_encode($row->username);
            $data['rows'][$i]['cell'][]= utf8_encode($row->email);
            $data['rows'][$i]['cell'][]= utf8_encode($row->phone_1);
            $data['rows'][$i]['cell'][]= utf8_encode($row->address_1);
            $data['rows'][$i]['cell'][]= utf8_encode($row->city);
            $data['rows'][$i]['cell'][]= utf8_encode($row->state);
            $data['rows'][$i]['cell'][]= 'oculto';
            $data['rows'][$i]['cell'][]= utf8_encode($row->usertype);
            $data['rows'][$i]['cell'][]= utf8_encode($row->extra_field_2);
            $data['rows'][$i]['cell'][]= utf8_encode($row->perms);
            $data['rows'][$i]['cell'][]= "<a href='".base_url()."index.php/admin/usuarios/generar_tarjeta/$row->id'>Generar Tarjeta<a/>";
            $i++;
        }
        echo json_encode($data);
    }



    function guardar_1() {
        $tabla          = "users";
        $oper           = $this->input->post('oper');
        $id             = $this->input->post('id');
        $name           = $this->input->post('name');
        $username       = $this->input->post('username');
        $email          = $this->input->post('email');
        $phone_1        = $this->input->post('phone_1');
        $address_1      = $this->input->post('address_1');
        $city           = $this->input->post('city');
        $state          = $this->input->post('state');
        $password       = $this->input->post('password');
        $usertype       = $this->input->post('usertype');
        $extra_field_2  = $this->input->post('extra_field_2');
        $perms          = $this->input->post('perms');
        if($oper=='edit') {
            if ($password!="oculto" && $password!="" ) {
                $query = $this->db->query("UPDATE $tabla set name='".$name."', username='".$username."', email='".$email."', password='".md5($password)."', phone_1='".$phone_1."', address_1='".$address_1."', city='".$city."', state='".$state."', usertype='".$usertype."', perms='".$perms."', extra_field_2='".$extra_field_2."' WHERE id=".$id);
            }else {
                $query = $this->db->query("UPDATE $tabla set name='".$name."', username='".$username."', email='".$email."', phone_1='".$phone_1."', address_1='".$address_1."', city='".$city."', state='".$state."', usertype='".$usertype."', perms='".$perms."', extra_field_2='".$extra_field_2."' WHERE id=".$id);
            }
        }elseif($oper=='add') {
            $query = $this->db->query("INSERT INTO $tabla set name='".$name."', username='".$username."', email='".$email."', password='".md5($password)."', phone_1='".$phone_1."', address_1='".$address_1."', city='".$city."', state='".$state."', usertype='".$usertype."', perms='".$perms."', extra_field_2='".$extra_field_2."'");
        }elseif($oper=='del') {
            $query = $this->db->query("DELETE FROM $tabla WHERE id=$id");
        }
    }

    function post2() {
        $tabla       = "min_modulos";
        $tabla2      = "min_permisos";
        $page        = $this->input->post('page');
        $limit       = $this->input->post('rows');
        $sidx        = $this->input->post('sidx');
        $sord        = $this->input->post('sord');
        $_search     = $this->input->post('_search');
        $searchField = $this->input->post('searchField');
        $searchString= $this->input->post('searchString');
        $id_usuario  = $this->uri->segment(4);

        $where = "";
        if ($_search=='true')
            $where = " WHERE $searchField LIKE '%$searchString%' ";


        if(!$sidx) $sidx =1;

        $sql    = "SELECT * FROM $tabla $where";
        //echo $sql;
        $query  = $this->db->query($sql);
        $result = $query->result();

        $count  = count($result);

        if( $count > 0 && $limit > 0) {
            $total_pages = ceil($count/$limit);
        } else {
            $total_pages = 0;
        }

        if ($page > $total_pages) $page=$total_pages;
        $start = $limit*$page - $limit;
        if($start <0) $start = 0;

        $sql    = "SELECT * FROM $tabla $where ORDER BY $sidx $sord LIMIT $start , $limit";
        $query  = $this->db->query($sql);
        // $result = $query->result();


        header("Expires: Mon, 26 Jul 1997 05:00:00 GMT" );
        header("Last-Modified: " . gmdate( "D, d M Y H:i:s" ) . "GMT" );
        header("Cache-Control: no-cache, must-revalidate" );
        header("Pragma: no-cache" );
        header("Content-type: text/x-json");
        $i=0;
        $data['page']= $page;
        $data['total']= $total_pages;

        foreach ($query->result() as $row) {
            $data['rows'][$i]['id']= $row->id;
            $data['rows'][$i]['cell'][]= $row->id;
            $data['rows'][$i]['cell'][]= utf8_encode($row->nombre);
            $data['rows'][$i]['cell'][]= utf8_encode($row->texto_menu);
            $data['rows'][$i]['cell'][]= utf8_encode($row->empresa);

            $query = $this->db->query("SELECT estado, modificar, eliminar FROM $tabla2 WHERE id_usuario=".$id_usuario." AND id_modulo=".$row->id);
            if ($query->num_rows() > 0) {
                $row=$query->row();
                if($row->estado=='si') {
                    $data['rows'][$i]['cell'][]= 'si';
                }else {
                    $data['rows'][$i]['cell'][]= 'no';
                }
                if($row->modificar=='si') {
                    $data['rows'][$i]['cell'][]= 'si';
                }else {
                    $data['rows'][$i]['cell'][]= 'no';
                }
                if($row->eliminar=='si') {
                    $data['rows'][$i]['cell'][]= 'si';
                }else {
                    $data['rows'][$i]['cell'][]= 'no';
                }
            }else {
                $data['rows'][$i]['cell'][]= 'no';
                $data['rows'][$i]['cell'][]= 'no';
                $data['rows'][$i]['cell'][]= 'no';
            }

            $i++;
        }
        echo json_encode($data);
    }

    function guardar_2() {
        $tabla          = "users";
        $tabla2         = "min_permisos";
        $oper           = $this->input->post('oper');
        $id             = $this->input->post('id');
        $modulo         = $this->input->post('nombre');//
        $empresa        = $this->input->post('empresa');//
        $estado         = $this->input->post('estado');
        $modificar      = $this->input->post('modificar');
        $eliminar       = $this->input->post('eliminar');
        $id_usuario     = $this->uri->segment(4);
//        echo $this->uri->segment(5);

        $query = $this->db->query("SELECT estado FROM $tabla2 WHERE id_usuario=".$id_usuario." AND id_modulo=".$id);
        if ($query->num_rows() > 0) {
            $query = $this->db->query("UPDATE $tabla2 set estado='".$estado."', modificar='".$modificar."', eliminar='".$eliminar."' WHERE id_usuario=".$id_usuario." AND id_modulo=".$id." AND empresa='".$empresa."'");
        }else {
            $query = $this->db->query("INSERT INTO $tabla2 set estado='".$estado."', id_usuario='".$id_usuario."', modulo='".$modulo."', empresa='".$empresa."', modificar='".$modificar."', eliminar='".$eliminar."', id_modulo=".$id);
        }
    }

    function todos() {
        $tabla          = "min_permisos";
        $tabla2         = "min_modulos";
        $id_usuario     = $this->uri->segment(4);
        $estado         = 'si';
        $modificar      = 'no';
        $eliminar       = 'no';

        $query = $this->db->query("SELECT * FROM $tabla2 ");

        foreach ($query->result() as $row) {

            $query = $this->db->query("SELECT estado FROM $tabla WHERE id_usuario=".$id_usuario." AND id_modulo=".$row->id);
            if ($query->num_rows() > 0) {
                $query = $this->db->query("UPDATE $tabla set estado='".$estado."' WHERE id_usuario=".$id_usuario." AND id_modulo=".$row->id." AND empresa='".$row->empresa."'");
            }else {
                $query = $this->db->query("INSERT INTO $tabla set estado='".$estado."', id_usuario='".$id_usuario."', modulo='".$row->nombre."', empresa='".$row->empresa."', modificar='".$modificar."', eliminar='".$eliminar."', id_modulo=".$row->id);
            }


        }
    }


}
?>